<?php 
/**
 * 附近门店导航小程序模块订阅器
 *
 * @author hellozjx
 * @url 
 */
defined('IN_IA') or exit('Access Denied');
class hellozjx_mapwxappModuleSite extends WeModuleSite
{
    public $table_shop = 'hellozjx_mapwxapp_shop';
	public $table_area = 'hellozjx_mapwxapp_area';
	public function doWebArea()
    {
        global $_W, $_GPC;
        load()->func('tpl');
        $operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
        if ($operation == 'display') {
            $pindex = max(1, intval($_GPC['page']));
            $psize = 20;
            $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_area) . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY displayorder DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_area) . " WHERE uniacid = '{$_W['uniacid']}'");
            $pager = pagination($total, $pindex, $psize);
        } elseif ($operation == 'post') {
            $id = intval($_GPC['id']);
            if (checksubmit('submit')) {
                $data = array('uniacid' => $_W['uniacid'], 'area_name' => $_GPC['area_name'], 'enabled' => intval($_GPC['enabled']), 'displayorder' => intval($_GPC['displayorder']), 'createtime' => TIMESTAMP);
                if (!empty($id)) {
                    unset($data['createtime']);
                    pdo_update($this->table_area, $data, array('id' => $id));
                } else {
                    pdo_insert($this->table_area, $data);
                    $id = pdo_insertid();
                }
                message('更新分类成功！', $this->createWebUrl('area', array('op' => 'display')), 'success');
            }
            $area = pdo_fetch("select * from " . tablename($this->table_area) . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
            if (empty($area)) {
                $area['displayorder'] = 0;
                $area['enabled'] = 1;
            }
        } elseif ($operation == 'delete') {
            $id = intval($_GPC['id']);
            $area = pdo_fetch("SELECT id FROM " . tablename($this->table_area) . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
            if (empty($area)) {
                message('抱歉，分类不存在或是已经被删除！', $this->createWebUrl('area', array('op' => 'display')), 'error');
            }
            pdo_delete($this->table_area, array('id' => $id));
            message('删除成功', referer(), 'success');
        }
        include $this->template('area');
    }
	public function doWebShop()
    {
		global $_W, $_GPC;
		$this->_check_config();
		$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		$areas = pdo_getall($this->table_area, array('uniacid' => $_W['uniacid'], 'enabled' => 1));
        $cates = array();
        $cates['0'] = '全部';
        if (!empty($areas)) {
            foreach ($areas as $key => $value) {
                $cates[$value['id']] = $value['area_name'];
            }
        }
		if ($operation == 'display') {
            $pindex = max(1, intval($_GPC['page']));
            $psize = 20;
            $keyword = trim($_GPC['keyword']);
            $condition = '';
            if (!empty($keyword)) {
                $condition = " AND name LIKE '%{$keyword}%'";
            }
            $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_shop) . " WHERE uniacid = '{$_W['uniacid']}' " . $condition . " ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_shop) . " WHERE uniacid = '{$_W['uniacid']}' " . $condition);
            $pager = pagination($total, $pindex, $psize);
        } elseif ($operation == 'post') {
            $id = intval($_GPC['id']);
            if (checksubmit('submit')) {
                $data = array(
				'uniacid' => $_W['uniacid'], 
				'name' => $_GPC['name'], 
				'area_id' => intval($_GPC['area_id']), 
				'logo' => $_GPC['logo'], 
				'thumb' => $_GPC['thumb'], 
				'enabled' => intval($_GPC['enabled']), 
				'displayorder' => intval($_GPC['displayorder']), 
				'phone' => $_GPC['phone'], 
				'address'=> $_GPC['address'], 
				'shop_lng' => $_GPC['location']['lng'], 
				'shop_lat' => $_GPC['location']['lat'], 
				'content' => htmlspecialchars_decode($_GPC['content']),
				'createtime' => TIMESTAMP
				);
				if($td_id>0)
				{
					$data['td_url']=$_GPC['td_url'];
				}
                load()->func('communication');
                $url = 'http://apis.map.qq.com/ws/coord/v1/translate?locations='.$data['shop_lat'].','.$data['shop_lng'].'&type=3&key=R2IBZ-3KOWO-OBWWY-SXICZ-4WOGH-WUFKO';
                $res = ihttp_get($url);
                $content = json_decode($res['content'], true);
                if ($content['status'] === 0) {
                    $data['shop_lng_t'] = $content['locations'][0]['lng'];
                    $data['shop_lat_t'] = $content['locations'][0]['lat'];
                } else {
                    $data['shop_lng_t'] = $data['shop_lng'];
                    $data['shop_lat_t'] = $data['shop_lat'];
                }
                if (!empty($id)) {
                    unset($data['createtime']);
                    pdo_update($this->table_shop, $data, array('id' => $id));
                } else {
                    pdo_insert($this->table_shop, $data);
                    $id = pdo_insertid();
                }
                message('更新信息成功！', $this->createWebUrl('shop', array('op' => 'display')), 'success');
            }
			//$td=pdo_getall($this->table_article,array('uniacid' => $_W['uniacid'], 'enabled' => 1),array('id', 'name', 'url'), '', array('id desc'));
            $shop = pdo_fetch("select * from " . tablename($this->table_shop) . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
        }elseif ($operation == 'delete') {
            $id = intval($_GPC['id']);
            $shop = pdo_fetch("SELECT id FROM " . tablename($this->table_shop) . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
            if (empty($shop)) {
                message('抱歉，私塾不存在或是已经被删除！', $this->createWebUrl('shop', array('op' => 'display')), 'error');
            }
            pdo_delete($this->table_shop, array('id' => $id));
            message('删除成功', referer(), 'success');
        }elseif ($operation == 'comment') {
			$id = intval($_GPC['id']);
            if ($_W['ispost']) {
                $method = 'hide';
                if (isset($_GPC['show'])) {
                    $method = 'show';
                }
                if (isset($_GPC['hide'])) {
                    $method = 'hide';
                }
                $ids = $_GPC['ids'];
                if (empty($ids)) {
                    message('请选择项目', referer(), 'error');
                }
				$num=count($ids);
                if ($method == 'show') {
                    pdo_update($this->table_comment, array('status' => 1), array('id in' => $ids));
					pdo_query('UPDATE ' . tablename($this->table_shop) . " SET comment = comment + :num WHERE id=:id", array(':id' => $id,':num'=>$num));
                } else {
                    pdo_update($this->table_comment, array('status' => 0), array('id in' => $ids));
					pdo_query('UPDATE ' . tablename($this->table_shop) . " SET comment = comment - :num WHERE id=:id", array(':id' => $id,':num'=>$num));
                }
				
                message('操作成功', referer(), 'success');
            }
            $shop = pdo_fetch("SELECT id FROM " . tablename($this->table_shop) . " WHERE id = '{$id}' AND uniacid=" . $_W['uniacid'] . "");
            if (empty($shop)) {
                message('抱歉，私塾不存在或是已经被删除！', $this->createWebUrl('shop', array('op' => 'display')), 'error');
            }
            $pindex = max(1, intval($_GPC['page']));
            $psize = 20;
            $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_comment) . " WHERE uniacid = '{$_W['uniacid']}' AND shop_id = '{$id}' ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_comment) . " WHERE uniacid = '{$_W['uniacid']}' AND shop_id = '{$id}'");
            $pager = pagination($total, $pindex, $psize);
        }
		include $this->template('shop');
	}

	private function _check_config()
    {
        $config = $this->module['config'];
        if (!$config) {
            message('请先进行系统配置', './index.php?c=profile&a=module&do=setting&m=hellozjx_mapwxapp', 'error');
        }
    }
	/*
	public function getCommmentOkNum($shop_id)
    {
        global $_W;
        return pdo_fetchcolumn('SELECT count(*) FROM ' . tablename($this->table_comment) . " WHERE uniacid=:uniacid AND shop_id=:shop_id AND status=1", array(":uniacid" => $_W['uniacid'], ':shop_id' => $shop_id)) ?: 0;
    }
	public function getCommmentNum($shop_id)
    {
        global $_W;
        return pdo_fetchcolumn('SELECT count(*) FROM ' . tablename($this->table_comment) . " WHERE uniacid=:uniacid AND shop_id=:shop_id", array(":uniacid" => $_W['uniacid'], ':shop_id' => $shop_id)) ?: 0;
    }
    */
}